package com.jinbaoty.recyclebins.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ImageUtils;
import com.camerakit.CameraKitView;
import com.jinbaoty.recyclebins.R;
import com.jinbaoty.recyclebins.utils.DataHelper;
import com.orhanobut.logger.Logger;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ImageUtil;

public class CameraKitActivity extends AppCompatActivity {

    private CameraKitView cameraKitView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        cameraKitView = findViewById(R.id.camera);
        Intent intent = getIntent();
        int cameraType = intent.getIntExtra("cameraType", 1);
        cameraKitView.setFacing(cameraType);
        cameraKitView.setCameraListener(new CameraKitView.CameraListener() {
            @Override
            public void onOpened() {
                Logger.d("相机打开");
            }

            @Override
            public void onClosed() {
                Logger.d("相机关闭");
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        cameraKitView.onStart();
        Logger.d("相机权限返回");


    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraKitView.onResume();

    }

    @Override
    protected void onPause() {
        cameraKitView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        cameraKitView.onStop();
        super.onStop();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        cameraKitView.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
    public void takePhoto(View view){
        cameraKitView.captureImage(new CameraKitView.ImageCallback() {
            @Override
            public void onImage(CameraKitView cameraKitView, byte[] bytes) {
                Intent intent = getIntent();
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                DataHelper.getInstance().saveData("photoData",bitmap);
                setResult(RESULT_OK,intent);
                finish();

            }
        });
    }

}
